package com.sfy.lotterysystem.util;

import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.web.servlet.HandlerInterceptor;

public class JwtInterceptor implements HandlerInterceptor {
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        String token = request.getHeader("Authorization");
        if (token != null && token.startsWith("Bearer ")) {
            token = token.substring(7);
        }
        if (token == null || !JwtUtil.validateToken(token)) {
            response.setStatus(401);
            response.getWriter().write("{\"error\":\"无效或缺失Token\"}");
            return false;
        }
        return true;
    }
}